<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      append-to-body
      :close-on-click-modal="false"
      @close="closeDialog()"
      v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="免费类型" prop="freeType">
            <el-select v-model="dataForm.freeType" clearable class="m-2" placeholder="免费类型">
              <el-option v-for="item in dicts.car_free_freeType" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停车场" prop="parkId">
            <el-select v-model="dataForm.parkId" class="m-2" placeholder="请选择停车场">
              <el-option v-for="item in parkOptions" :key="item.id" :label="item.parkName" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input v-model="dataForm.plateNumber" placeholder="车牌号"></el-input>
      </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/car/carfree'
import {fetchList as getParkListByDept} from "@/api/stop/parking";

export default {
  dicts: ['car_free_freeType'],
  data() {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        id: '',
        plateNumber: '',
        freeType: '',
        parkId: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: '',
        remark: '',
      },
      dataRule: {
        plateNumber: [
          {required: true, message: '车牌号不能为空', trigger: 'blur'}
        ],

        freeType: [
          {required: true, message: '请选择免费类型', trigger: 'blur'}
        ],

        parkId: [
          {required: true, message: '请选择停车场', trigger: 'blur'}
        ],

      },
      parkOptions: []
    }
  },
  methods: {
    init(id) {
      getParkListByDept().then(response => {
        this.parkOptions = response.data.data.records
      })
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog() {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
